package com.demon.androidbasic.action

import android.os.Bundle
import android.os.Parcelable
import java.io.Serializable
import java.util.*

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2019/10/23
 * desc   : 参数意图
 */
interface BundleAction {
    val bundle: Bundle?
    fun getInt(name: String?): Int {
        return getInt(name, 0)
    }

    fun getInt(name: String?, defaultValue: Int): Int {
        return if (bundle == null) defaultValue else bundle!!.getInt(name, defaultValue)
    }

    fun getLong(name: String?): Long {
        return getLong(name, 0)
    }

    fun getLong(name: String?, defaultValue: Int): Long {
        return if (bundle == null) defaultValue.toLong() else bundle!!.getLong(
            name,
            defaultValue.toLong()
        )
    }

    fun getFloat(name: String?): Float {
        return getFloat(name, 0)
    }

    fun getFloat(name: String?, defaultValue: Int): Float {
        return if (bundle == null) defaultValue.toFloat() else bundle!!.getFloat(
            name,
            defaultValue.toFloat()
        )
    }

    fun getDouble(name: String?): Double {
        return getDouble(name, 0)
    }

    fun getDouble(name: String?, defaultValue: Int): Double {
        return if (bundle == null) defaultValue.toDouble() else bundle!!.getDouble(
            name,
            defaultValue.toDouble()
        )
    }

    fun getBoolean(name: String?): Boolean {
        return getBoolean(name, false)
    }

    fun getBoolean(name: String?, defaultValue: Boolean): Boolean {
        return if (bundle == null) defaultValue else bundle!!.getBoolean(name, defaultValue)
    }

    fun getString(name: String?): String? {
        return if (bundle == null) null else bundle!!.getString(name)
    }

    fun <P : Parcelable?> getParcelable(name: String?): P? {
        return if (bundle == null) null else bundle!!.getParcelable(name)
    }

    fun <S : Serializable?> getSerializable(name: String?): S? {
        return (if (bundle == null) null else bundle!!.getSerializable(name)) as S?
    }

    fun getStringArray(name: String): Array<String>? {
        return if (bundle == null) null else bundle!!.getStringArray(name)
    }

    fun getStringArrayList(name: String): ArrayList<String>? {
        return if (bundle == null) null else bundle!!.getStringArrayList(name)
    }

    fun getIntegerArrayList(name: String?): ArrayList<Int?>? {
        return if (bundle == null) null else bundle!!.getIntegerArrayList(name)
    }
}